package Servlet.User;
//注册
import DataBase.DataBase;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 注册成功，注册账号进入数据库中
 */
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取输入名字
        String name = req.getParameter("name");
        //获取输入id
        String username = req.getParameter("username");
        //获取输入密码
        String password = req.getParameter("password");
        //获取输入验证码
        String VerificationCode = req.getParameter("VerificationCode").toUpperCase();
        //获取实际验证码
        HttpSession HS = req.getSession();
        String trueVerificationCode = HS.getAttribute("VCode").toString().toUpperCase();

        if(VerificationCode.equals(trueVerificationCode)){
            if(DataBase.HaveRegister(username)){
                DataBase.SetNewUsernameAndPassword(username,password,name);
                resp.sendRedirect("index.jsp");
            }
            else{
                resp.sendRedirect("User/Register.jsp");
            }
        }
        else{
            resp.sendRedirect("User/Register.html");
            System.out.println("验证码错误");
        }
    }
}
